package com.chy.blog_server.modular.web.controller;

import cn.hutool.core.util.StrUtil;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.modular.web.entity.BoLoveList;
import com.chy.blog_server.modular.web.service.IBoLoveListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：恋爱清单表 controller
 * 作者：chenhuiyan
 * 时间：2020/04/10
 */
@Controller
@RequestMapping(value = "/web/boLoveList")
public class BoLoveListController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boLoveList/";

    @Autowired
    private IBoLoveListService boLoveListService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boLoveList";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boLoveListService.list(params); //获取数据列表
            Integer count = boLoveListService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boLoveList_add";
    }

    /**
     * 保存
     * @param boLoveList
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> saveData(BoLoveList boLoveList){
        Map<String, Object> res = new HashMap<>();
        try {
            if(StrUtil.isEmpty(boLoveList.getId())){
                boLoveList.preInsert();
                boLoveListService.save(boLoveList);
                res.put("state", "200");
                res.put("message", "添加成功");
            }else{
                boLoveListService.updateById(boLoveList);
                res.put("state", "200");
                res.put("message", "修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }

    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boLoveListService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            BoLoveList boLoveList = boLoveListService.getById(id);
            boLoveList.setDelFlag(1);
            boLoveListService.updateById(boLoveList);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

}
